package com.dwxt.cdms.service;

import com.dwxt.cdms.admin.service.UserServiceAdmin;
import com.dwxt.cdms.base.Page;
import com.dwxt.cdms.dao.PartMapper;
import com.dwxt.cdms.entity.Part;
import com.dwxt.cdms.entity.PartExample;
import com.dwxt.common.base.BaseResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PartService {

    @Autowired
    private PartMapper partMapper;

    @Autowired
    private UserServiceAdmin userServiceAdmin;

    public BaseResult page(Page page) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        PartExample example = new PartExample();
        List<Part> partList = partMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo(partList);
        return BaseResult.ok(pageInfo);
    }

    public BaseResult list(Integer customerId) {
        PartExample example = new PartExample();
        PartExample.Criteria criteria = example.createCriteria();
        if (customerId!=null){
            criteria.andCustomerIdEqualTo(customerId);
        }
        List<Part> partList = partMapper.selectByExample(example);
        return BaseResult.ok(partList);
    }

    public BaseResult add(Part part) {
        partMapper.insertSelective(part);
        return BaseResult.ok();
    }

    public BaseResult delete(Integer id) {
        partMapper.deleteByPrimaryKey(id);
        return BaseResult.ok();
    }

    public BaseResult update(Part part) {
        partMapper.updateByPrimaryKeySelective(part);
        return BaseResult.ok();
    }
}
